package calculate24.play.SelfCalculate;

import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.content.res.AppCompatResources;

import java.util.ArrayList;

import calculate24.play.R;
import calculate24.play.SelfCalculate.CardsHistory.CardsHistory;
import calculate24.play.common.card.Card;

public class SelfCalculate extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_self_calculate);
        LinearLayout cardZone = findViewById(R.id.selfCalculateCardZone);
        Button backButton = findViewById(R.id.selfCalculateRollBackButton);
        Button forwardButton = findViewById(R.id.selfCalculateRollForwardButton);

        //生成四张卡片和它的结果集
        Card[] cards = new Card[4];
        ValidInput input = new ValidInput();
        int[] cardViewIDs = new int[] {R.id.selfCalculateCard1, R.id.selfCalculateCard2, R.id.selfCalculateCard3, R.id.selfCalculateCard4};
        for(int i = 0; i < 4; ++i) {
            cards[i] = new Card(findViewById(cardViewIDs[i]), input.numbers[i]);
            cards[i].view.setImageResource(input.imageIDs[i]);
        }

        //卡片选择区
        ArrayList<Card> pickedCards = new ArrayList<Card>(2);
        Drawable highLightFrame = AppCompatResources.getDrawable(this, R.drawable.high_light_frame);
        CardsHistory saveCardStatus = new CardsHistory(cards);
        for(int i = 0; i < 4; ++i) {
            cards[i].setOnPickListener(new Card.OnPickListener() {
                @Override
                public void onPick(Card card) {
                    pickedCards.add(card);
                    card.view.setForeground(highLightFrame);
                    if(pickedCards.size() == 2) {
                        //弹窗给用户选择运算
                        Card card1 = pickedCards.get(0);
                        Card card2 = pickedCards.get(1);
                        PopupCalculationMenu.create(SelfCalculate.this, card1, card2,
                                new PopupCalculationMenu.onResultListener() {
                                    @Override
                                    public void onResult(int result) {
                                        card1.showNumber(result);
                                        card1.drop();
                                        card2.drop();
                                        card2.view.setVisibility(View.INVISIBLE);
                                        saveCardStatus.save();
                                        backButton.setEnabled(true);
                                        forwardButton.setEnabled(false);
                                    }
                                });
                    }
                }

                @Override
                public void onDrop(Card card) {
                    pickedCards.remove(card);
                    card.view.setForeground(null);
                }

                @Override
                public boolean canPick(Card card) {
                    return pickedCards.size() < 2;
                }
            });
        }

        //回退按钮，前进按钮
        backButton.setEnabled(false);
        forwardButton.setEnabled(false);
        backButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveCardStatus.back();
                forwardButton.setEnabled(true);
                if(!saveCardStatus.canBack()) {
                    view.setEnabled(false);
                }
            }
        });
        forwardButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                saveCardStatus.forward();
                backButton.setEnabled(true);
                if(!saveCardStatus.canForward()) {
                    view.setEnabled(false);
                }
            }
        });

        //"看答案"按钮
        Button sadButton = findViewById(R.id.selfCalculateSadButton);
        sadButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                SolutionsDialog.create(SelfCalculate.this, input.resultSet);
            }
        });
    }

}
